import sys
import os
import re
import json
import urlparse
import logging

import connector.connector
import settings


log = logging.getLogger(settings.log_name_applog)


def __print_completed_filename():
    filepath = connector.connector.get_latest_downloader_completed_log_filepath()
    if filepath and filepath is not 'EMPTY':
        filestream = open(filepath, 'r')
        urls = filestream.read()
        filestream.close()

        for filename in os.listdir(settings.download_save_path):
            if filename.split('.')[-1] == 'json':
                filepath = settings.download_save_path + filename
                filestream = open(filepath, 'r')
                json_content = json.load(filestream)
                pattern = urlparse.urlparse(json_content['webpage_url']).query
                if re.search(pattern, urls) is not None:
                    title = json_content['title']
                    msg = 'downloaded: ' + title
                    sys.stdout.write(msg + '\n')
                    #NOTICE: msg is not logged.
                    log.info(msg)

                    filestream.close()

    else:
        print 'No video has been downloaded.'
        return


def processor():
    filestream = open(connector.connector.get_latest_app_log_filepath())
    content = filestream.read()
    print content
    filestream.close()

    completed_list = [entry.lstrip(settings.downloader_logging_file_completed_pattern)
                      for entry in re.findall(settings.downloader_logging_file_completed_pattern + '.*', content)]
    failed_list = [entry.lstrip(settings.downloader_logging_file_failed_pattern)
                   for entry in re.findall(settings.downloader_logging_file_failed_pattern + '.*', content)]

    completed_result = '\n'.join(completed_list)
    failed_result = '\n'.join(failed_list)

    if completed_result:
        connector.connector.init_latest_downloader_completed_log_filepath()
        result = completed_result.lstrip(settings.downloader_logging_file_completed_pattern)

        filestream = open(connector.connector.get_latest_downloader_completed_log_filepath(), 'a')
        filestream.write(result)
        filestream.close()

        filestream = open(settings.archive_filename, 'a')
        filestream.write(result)
        filestream.close()

    if failed_result:
        connector.connector.init_latest_downloader_failed_log_filepath()
        filestream = open(connector.connector.get_latest_downloader_failed_log_filepath(), 'a')
        filestream.write(failed_result.lstrip(settings.downloader_logging_file_failed_pattern))
        filestream.close()

    __print_completed_filename()

if __name__ == '__main__':
    processor()